package canglang.monitor.cases.action;

import canglang.monitor.cases.model.CaseTenModel;
import canglang.monitor.cases.service.CaseTenMgr;
import canglang.monitor.cases.to.SearchTenTO;
import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.util.Date;
import java.util.List;
import java.io.OutputStream;

@SuppressWarnings("unchecked")
public class CaseTenAction extends BaseAction {
    private static final long serialVersionUID = -4488733076279225838L;
     private CaseTenModel caseTenModel;

    private CaseTenMgr caseTenMgr;

    private PaginatedListHelper helper;

    private SearchTenTO searchTO;

    public CaseTenModel getCaseTenModel() {
        return caseTenModel;
    }

    public void setCaseTenModel(CaseTenModel caseTenModel) {
        this.caseTenModel = caseTenModel;
    }

    public CaseTenMgr getCaseTenMgr() {
        return caseTenMgr;
    }

    public void setCaseTenMgr(CaseTenMgr caseTenMgr) {
        this.caseTenMgr = caseTenMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchTenTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTenTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = CaseTenModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

    public String search() {
        if (searchTO == null) {
            searchTO = new SearchTenTO();
        }
        UserModel user=(UserModel)getRequest().getSession().getAttribute("user");
        if(!user.getUserRole().equalsIgnoreCase("A")){
            searchTO.setCreateId(String.valueOf(user.getDeptId()));
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("CREATE_DATE");
        }
        helper = caseTenMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String delete() {
        for (int i = 0; i < helper.getList().size(); i++) {
            CaseTenModel u = (CaseTenModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                caseTenMgr.deleteCaseTen(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return search();
    }

    public String add() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseTenModel.setCreateId(String.valueOf(user.getDeptId()));
        caseTenModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseTenModel.setCreateDt(date);
        caseTenModel.setUpdateDt(date);
        caseTenModel.setUploadCnt(FileUtil.convertToByte(caseTenModel.getUpload()));
        caseTenModel.setUploadFileName(caseTenModel.getUploadFileName());
        caseTenModel=caseTenMgr.addCaseTen(caseTenModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String view() {
        String selectId = caseTenModel.getSelectId();
        caseTenModel= caseTenMgr.getCaseTen(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String save() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseTenModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseTenModel.setUpdateDt(date);
        if(StringUtil.isNullOrBlank(caseTenModel.getUploadFileName())){
            CaseTenModel c=caseTenMgr.getCaseTen(caseTenModel.getCaseTenId());
            caseTenModel.setUploadFileName(c.getUploadFileName());
            caseTenModel.setUploadCnt(c.getUploadCnt());
        }else{
            caseTenModel.setUploadCnt(FileUtil.convertToByte(caseTenModel.getUpload()));
        }
        caseTenModel=caseTenMgr.saveCaseTen(caseTenModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

    public String download() throws Exception{
        try {
            String selectId = caseTenModel.getSelectId();
            caseTenModel= caseTenMgr.getCaseTen(Integer.parseInt(selectId));
            getResponse().reset();
            getResponse().setHeader(
                    "Content-Disposition",
                    "attachment; filename=\""
                            + new String(caseTenModel.getUploadFileName().getBytes("gb2312"), "ISO8859-1"));
            getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
            byte[] content = caseTenModel.getUploadCnt();
            getResponse().setContentLength(content.length);
            OutputStream out = getResponse().getOutputStream();
            out.write(content);
            out.flush();
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }
}